package showtime.framework.manager
{
	import showtime.framework.core.pattern.Singleton;
	import showtime.framework.net.rpc.BaseRpc;
	import showtime.struct.ArrayCollection;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.getClassByAlias;
	import flash.net.registerClassAlias;
	
	/** 
	 * @project showtime
	 * @author yinhao
	 * @date 2011-1-7
	 *
	 */
	
	public class RpcManager extends Singleton
	{
		private var rpcMap:Object = null;
		
		public function RpcManager()
		{
			super();
			
			initialize();
		}
		
		static public function getInstance():RpcManager
		{
			return RpcManager(Singleton.getInstanceOrCreate(RpcManager));
			
		}
		
		/**
		 * Pre initialize. 
		 * 
		 */		
		override protected function initialize():void
		{
			super.initialize();
		
//			registerClassAlias("flex.messaging.messages.ErrorMessage", ErrorMessage);
//			registerClassAlias("flex.messaging.messages.CommandMessage", CommandMessage);
//			registerClassAlias("flex.messaging.messages.RemotingMessage", RemotingMessage);
//			registerClassAlias("flex.messaging.messages.AcknowledgeMessage ", AcknowledgeMessage);
//			registerClassAlias("DSC", CommandMessageExt);
//			registerClassAlias("DSK", AcknowledgeMessageExt);
//			registerClassAlias("flex.messaging.config.ConfigMap", ConfigMap);
//			registerClassAlias("flex.messaging.io.ArrayCollection", ArrayCollection);
//			registerClassAlias("flex.messaging.io.ObjectProxy", ObjectProxy);
			
			try
			{
				if (getClassByAlias("flex.messaging.io.ArrayCollection") == null)
				{
					registerClassAlias("flex.messaging.io.ArrayCollection", ArrayCollection);				
				}
			}
			catch(e:ReferenceError)
			{
				registerClassAlias("flex.messaging.io.ArrayCollection", ArrayCollection);		
			}
			
			
			rpcMap = {};
		}
		
		public function loadFromXML(path:String):void
		{
			var urlLoader:URLLoader = new URLLoader();
			
			urlLoader.load(new URLRequest(path));
			
			urlLoader.addEventListener(Event.COMPLETE, loadCompleteHandler);
			
			function loadCompleteHandler(event:Event):void
			{
				urlLoader.removeEventListener(Event.COMPLETE, loadCompleteHandler);
				// Analysis xml data.
				xmlAnalysis(new XML(urlLoader.data));
			}
		}
		
		private function xmlAnalysis(xml:XML):void
		{
			for each (var element:XML in xml.rpcConfig.rpc)
			{
				var name:String = element.@name;
				var endpoint:String = element.@endpoint;			
				
				buildRpc(name, endpoint);
			}
		}
		
		/** 
		 * Build socket connection. 
		 * 
		 */		
		public function buildRpc(name:String, endpoint:String):BaseRpc
		{
			var rpc:BaseRpc = new BaseRpc(endpoint);

			rpcMap[name] = rpc;
			
			return rpc;
		}
		
		/**
		 * Get rpc service proxy.
		 *  
		 * @param serviceName
		 * @return 
		 * 
		 */		
		public function getRpc(name:String, serviceName:String = null):BaseRpc
		{
			rpcMap[name].destination = serviceName;
			
			return rpcMap[name];
		}
		
	}
}